public class Solution640 {
    public String solveEquation(String equation) {
        int t=equation.indexOf("=");
        int[] s1=solve(equation.substring(0,t));
        int[] s2=solve(equation.substring(t+1));
        int a=s1[0]-s2[0];
        int b=s2[1]-s1[1];
        if (a==0){
            if (b==0) {
                return "Infinite solutions";
            }else{
                return "No solution";
            }
        }else {
            return "x=".concat(String.valueOf(b/a));
        }
    }

    private int[] solve(String s) {
        int a=0,b=0,cur=0;
        if (!s.startsWith("-")){
            s="+".concat(s);
        }
        while (cur<s.length()){
            int flag;
            if (s.charAt(cur)=='+'){
                flag=1;
            }else {
                flag=-1;
            }
            cur++;
            int start=cur;
            while (cur<s.length()&&s.charAt(cur)!='+'&&s.charAt(cur)!='-'){
                cur++;
            }
            String temp=s.substring(start,cur);
            if (temp.endsWith("x")){
                if (temp.length()==1){
                    a+=flag;
                }else{
                    a+=flag*Integer.parseInt(temp.substring(0,temp.length()-1));
                }
            }else{
                b+=flag*Integer.parseInt(temp);
            }
        }
        return new int[]{a,b};
    }

    public static void main(String[] args) {
        System.out.println(new Solution640().solveEquation("x+5-3+x=6+x-2"));
    }
}
